<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="deviceName" style="width: 308px">
        <el-input
            v-model="queryParams.deviceName"
            placeholder="请输入设备名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="告警事件" prop="title" style="width: 308px">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入告警事件"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发生时间" prop="createTime" style="width: 308px">
        <el-date-picker clearable
                        v-model="daterangeCreateTime"
                        value-format="YYYY-MM-DD"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处理状态" prop="handleStatus" style="width: 308px">
        <el-select v-model="queryParams.handleStatus" placeholder="请选处理状态" clearable style="width: 308px">
          <el-option
              v-for="dict in handle_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理人" prop="handler" style="width: 308px">
        <el-input
          v-model="queryParams.handler"
          placeholder="请输入处理人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理时间" prop="handleTime" style="width: 308px">
        <el-date-picker clearable
                        v-model="daterangeHandleTime"
                        value-format="YYYY-MM-DD"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Check"
            :disabled="multiple"
            @click="handleAlarmAll"
            v-hasPermi="['coal:alarmEvent:handle']"
        >批量处理</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmEventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" :selectable="checkboxValidate" />
      <el-table-column label="类型" align="center" prop="eventType" width="80">
        <template #default="scope">
          <dict-tag :options="alarm_event_type" :value="scope.row.eventType" />
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName"  :show-overflow-tooltip="true"  width="150" />
      <el-table-column label="告警事件" align="center" prop="title"  :show-overflow-tooltip="true" width="100"/>
      <el-table-column label="详细信息" align="center" prop="content"  :show-overflow-tooltip="true" width="100"/>
      <el-table-column label="事件级别" align="center" prop="eventLevel" width="80">
        <template #default="scope">
          <dict-tag :options="alarm_level" :value="scope.row.eventLevel" />
        </template>
      </el-table-column>

      <el-table-column label="发生时间" align="center" prop="createTime" width="170">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="handleStatus" width="80">
        <template #default="scope">
          <dict-tag :options="handle_status" :value="scope.row.handleStatus" />
        </template>
      </el-table-column>
      <el-table-column label="处理人" align="center" prop="handler" :show-overflow-tooltip="true" width="100"/>
      <el-table-column label="处理时间" align="center" prop="handleTime" width="170">
        <template #default="scope">
          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理备注" align="center" prop="handleResult" :show-overflow-tooltip="true" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              link
              type="primary"
              icon="Promotion"
              v-if="scope.row.handleStatus === '0'"
              @click="handleAlarm(scope.row)"
              v-hasPermi="['coal:alarmEvent:handle']"
          >处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改事件对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="alarmEventRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="deviceId">
          {{ form.deviceName }}
        </el-form-item>
        <el-form-item label="告警事件" prop="title">
          {{ form.title }}
        </el-form-item>
        <el-form-item label="详细信息">
          {{ form.content }}
        </el-form-item>
        <el-form-item label="处理备注" prop="handleResult">
          <el-input v-model="form.handleResult" type="textarea" :rows="3" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AlarmEvent">
import { listAlarmEvent, getAlarmEvent, handleOneAlarmEvent,handleMoreAlarmEvent } from "@/api/coal/alarmEvent";

const { proxy } = getCurrentInstance();
const { alarm_level,alarm_event_type,handle_status } = proxy.useDict("alarm_level","alarm_event_type","handle_status");

const alarmEventList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreateTime = ref([]);
const daterangeHandleTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    content: null,
    deviceId: null,
    deviceName: null,
    thresholdDataId: null,
    eventLevel: null,
    eventType: null,
    handler: null,
    handleStatus: null,
    handleTime: null,
    handleResult: null,
    createTime: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询事件列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  if (null != daterangeHandleTime && '' != daterangeHandleTime) {
    queryParams.value.params["beginHandleTime"] = daterangeHandleTime.value[0];
    queryParams.value.params["endHandleTime"] = daterangeHandleTime.value[1];
  }
  listAlarmEvent(queryParams.value).then(response => {
    alarmEventList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    content: null,
    deviceId: null,
    thresholdDataId: null,
    eventLevel: null,
    eventType: null,
    handler: null,
    handleStatus: null,
    handleTime: null,
    handleResult: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("alarmEventRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = [];
  daterangeHandleTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 处理按钮操作 */
function handleAlarm(row) {
  reset();
  const _id = row.id || ids.value
  getAlarmEvent(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "处理事件";
  });
}

function handleAlarmAll(){
  reset();
  const _ids = ids.value;
  proxy.$modal.confirm('是否确认批量处理勾选的事件？').then(function() {
    return handleMoreAlarmEvent(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("处理成功");
  }).catch(() => {
    getList();
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["alarmEventRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        handleOneAlarmEvent(form.value).then(response => {
          proxy.$modal.msgSuccess("处理成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('coal/alarmEvent/export', {
    ...queryParams.value
  }, `alarmEvent_${new Date().getTime()}.xlsx`)
}

const route = useRoute();
onMounted(() => {
  if (route.params && route.params.messageSourceId){
    queryParams.value.id = route.params.messageSourceId
  }
  getList();
})

function checkboxValidate(row, index){
  return row.handleStatus === '0';
}

</script>
